package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1PolyMorph;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ItemActionPoly {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  public static boolean forNpcQuest(String s, L1PcInstance pc) {
    ArrayList<Object> aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (((String)aTempData.get(0)).equals(s) && pc.getLevel() >= ((Integer)aTempData.get(1)).intValue()) {
        if (!pc.getInventory().checkItem(((Integer)aTempData.get(3)).intValue(), (
            (Integer)aTempData.get(4)).intValue())) {
          L1Item temp = ItemTable.get().getTemplate(((Integer)aTempData.get(3)).intValue());
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(temp.getNameId())) + "不足( " + (
                (Integer)aTempData.get(4)).intValue() + ")，无法使用。"));
          return false;
        } 
        pc.getInventory().consumeItem(((Integer)aTempData.get(3)).intValue(), (
            (Integer)aTempData.get(4)).intValue());
        L1PolyMorph.doPoly((L1Character)pc, ((Integer)aTempData.get(2)).intValue(), 1800, 1);
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
      } 
      i++;
    } 
    return false;
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_自订变形卷轴");
      ArrayList<Object> aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          sTemp = rset.getString("action");
          aReturn.add(0, sTemp);
          aReturn.add(1, Integer.valueOf(rset.getInt("checkLevel_min")));
          aReturn.add(2, new Integer(rset.getInt("checkPoly")));
          aReturn.add(3, new Integer(rset.getInt("扣除道具编号")));
          aReturn.add(4, new Integer(rset.getInt("扣除道具数量")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
